package com.kun.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.kun.entity.*;
import com.kun.service.putBookInfoService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/PutBook")
public class PutBookController {
    @Autowired
    private putBookInfoService putBookInfoService;
    @GetMapping("/PutBook/{putbookId}")
    public ResponseJson<PutbookInfo> findById(@PathVariable("putbookId")Integer putbookId){
        PutbookInfo putbookInfo =putBookInfoService.findById(putbookId);
        ResponseJson<PutbookInfo> result = new ResponseJson<>();
        if(putbookInfo == null){
            result.setCode(500);
            result.setMsg("未找到数据");
        }else{
            result.setCode(200);
            result.setMsg("找到数据");
            result.setData(putbookInfo);
        }
        return result;
    }
    @GetMapping("/findByPage")
    public ResponseJson<Map> findByPages(@RequestParam("page") Integer pageNum
            , @RequestParam("limit") Integer pageSize
            , PutbookInfo putbookInfo){
        Map<String, Object> data = putBookInfoService.selectByPage(pageNum, pageSize,putbookInfo);
        ResponseJson<Map> result = new ResponseJson<>();
        if (data != null){
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(data);
        }else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }
  @PostMapping("/insert")
    public  ResponseJson<PutbookInfo> insertPutBook(PutbookInfo putbookInfo){
        ResponseJson<PutbookInfo> result = new ResponseJson<>();
        int i = putBookInfoService.insertPutBook(putbookInfo);
        if (i==0){
            result.setCode(500);
            result.setMsg("添加失败");
        }else{
            result.setCode(200);
            result.setMsg("添加成功");
        }
        return result;
    }
    @PutMapping("/updatePutBook")
    public ResponseJson updatePutBook(PutbookInfo putbookInfo){
        ResponseJson<PutbookInfo> result = new ResponseJson<>();
        if (putBookInfoService.updatePutBook(putbookInfo)>0){
            result.setCode(200);
            result.setMsg("更新成功");
        }else{
            result.setCode(500);
            result.setMsg("更新失败");
        }
        return result;
    }
    @DeleteMapping("/batchDelete")
    public ResponseJson batchDelete(Integer[] putbookIds){
        ResponseJson<PutbookInfo> result = new ResponseJson<>();
        if (putBookInfoService.deletePutBook(putbookIds)>0){
            result.setCode(200);
            result.setMsg("删除成功");
        }else{
            result.setCode(500);
            result.setMsg("删除失败");
        }
        return result;
    }
    @GetMapping("/exportExcel")
    public void exportExcel(PutbookInfo putbookInfo, HttpServletResponse response) throws IOException {
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode("入库记录.xls","UTF-8"));

        List<PutbookInfo> list = putBookInfoService.exportExcel(putbookInfo);

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("入库记录", "入库数据"),
                PutbookInfo.class, list);

        ServletOutputStream out = response.getOutputStream();

        workbook.write(out);

        out.flush();

        out.close();
    }
}
